global class AT_ProcessAllDLogsType implements Database.Batchable<Sobject>
{
	global string query;

	global AT_ProcessAllDLogsType(string q)
	{
		query = q;
	}
	
	global AT_ProcessAllDLogsType()
	{
		query = 'Select ID, Transaction_Type__c, Request_Text__c From AT_Debug_Log__c where Transaction_Type__c=null or Transaction_Type__c=\'\'';
		//query = 'Select RecordType.Name, RecordTypeId, Id, Latest_Fund_Amount__c, Latest_Fund_Amount_Rank__c, Latest_Fund_Amount_Rank_X_Wtg__c From Account where RecordType.DeveloperName=\'Investor\' and Latest_Fund_Amount__c>0 order by Latest_Fund_Amount__c';
	}

	global Database.QueryLocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator(query);
	}
	
	global void execute(Database.BatchableContext BC, List<sObject> scope)
	{
		
		List<AT_Debug_Log__c> listItem2Upd = new List<AT_Debug_Log__c>();

		for(sObject item:scope)
		{
			AT_Debug_Log__c tmpItem = (AT_Debug_Log__c)item;
			String ResStr = tmpItem.Request_Text__c;
			String TType = 'Other';
			if(ResStr.contains('<transactiontype>FastSpring Sales Order</transactiontype>')){ TType = 'Sales Order';}
			if(ResStr.contains('<transactiontype>FastSpring Sales Invoice</transactiontype>')){ TType = 'Sales Invoice';}
			tmpItem.Transaction_Type__c = TType;
			listItem2Upd.add(tmpItem);
		}
		if(listItem2Upd.size()>0) update listItem2Upd;
	}
	
	global void finish(Database.BatchableContext BC)
	{
		
	}	
	
	public static testMethod void testIt()
	{

		AT_Debug_Log__c DLog = new AT_Debug_Log__c();
		DLog.Request_Text__c = '<?xml version="1.0" encoding="UTF-8"?>'
			+ '<request>'
			+ '<control>'
			+ '<transactiontype>FastSpring Sales Order</transactiontype>'
			+ '</control>'
			+ '</request>';
		DLog.Response_Text__c = '';
		DLog.Status__c = 'Success';
		DLog.Transaction_Key__c = null;
		insert DLog;
		
		AT_Debug_Log__c DLog2 = new AT_Debug_Log__c();
		DLog2.Request_Text__c = '<?xml version="1.0" encoding="UTF-8"?>'
			+ '<request>'
			+ '<control>'
			+ '<transactiontype>FastSpring Sales Invoice</transactiontype>'
			+ '</control>'
			+ '</request>';
		DLog2.Response_Text__c = '';
		DLog2.Status__c = 'Success';
		DLog2.Transaction_Key__c = null;
		insert DLog2;
		
		AT_Debug_Log__c DLog3 = new AT_Debug_Log__c();
		DLog3.Request_Text__c = '<?xml version="1.0" encoding="UTF-8"?>'
			+ '<request>'
			+ '<control>'
			+ '<transactiontype>123</transactiontype>'
			+ '</control>'
			+ '</request>';
		DLog3.Response_Text__c = '';
		DLog3.Status__c = 'Success';
		DLog3.Transaction_Key__c = null;
		insert DLog3;

		List<sObject> list2Test = new List<sObject>();
		list2Test.add(DLog);
		
		System.Test.startTest();
		
		AT_ProcessAllDLogsType controller = new AT_ProcessAllDLogsType();
		controller = new AT_ProcessAllDLogsType('Select ID, Status__c, Response_Text__c, Request_Text__c From AT_Debug_Log__c where Status__c=\'Success\' LIMIT 3');
		
		Database.BatchableContext BC2 = null;
		controller.start(BC2);
		controller.execute(BC2,list2Test);
		controller.finish(BC2);

		System.Test.stopTest();

	}
}